/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Models.DAOs;

import DBConnection.ConnectionFactory;
import Models.Books;
import Models.Products;
import Security.Encrypter;
import java.sql.*;
import java.util.ArrayList;
import Security.Master_check;

public class Book_DAO extends Product_DAO {
 
    public boolean addBook(Books book){
        try{
                      
            //error checking here for regex.
            //if(error checking fails)
            //return false
            //same with updatebooks
            Master_check checker = new Master_check();
            if(checker.bookcheck(book))
            {
             
                int productID = this.addProduct(book);
                ConnectionFactory myFactory = ConnectionFactory.getFactory();
                Connection conn = myFactory.getConnection();
                int i = 1;

                PreparedStatement pstmtBook = conn.prepareStatement("INSERT INTO books(isbn, author, publisher, category, productID) VALUES(?,?,?,?,?)");
                pstmtBook.setString(i++, book.getIsbn());
                pstmtBook.setString(i++, book.getAuthor());
                pstmtBook.setString(i++, book.getPublisher());
                pstmtBook.setString(i++, book.getCategory());
                pstmtBook.setInt(i++, productID);
                pstmtBook.executeUpdate();
                return true;
            }
            else{
                return false;
            }
        }
        catch(Exception e){
            return false;
        }
    }
    
    public boolean updateBook(Books book,String bookCode){
        try{
            Master_check checker = new Master_check();
            if(checker.bookcheck(book))
            {
            
            String encryptedBook = new Encrypter().decrypt(bookCode);
            book.setProductID(Integer.parseInt(encryptedBook));
            
            boolean updatedProduct = this.updateProduct(book);
            
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            int i = 1;
           
            PreparedStatement pstmtBook = conn.prepareStatement("UPDATE books SET isbn = ?, author = ?, publisher = ?, category = ? WHERE productID = ?");
            pstmtBook.setString(i++, book.getIsbn());
            pstmtBook.setString(i++, book.getAuthor());
            pstmtBook.setString(i++, book.getPublisher());
            pstmtBook.setString(i++, book.getCategory());
            pstmtBook.setInt(i++, book.getProductID());
            //shouldn't we add price, etc?
            pstmtBook.executeUpdate();
            return true;
            }
            else
                return false;
        }
        catch(Exception e){
            
            return false;
        }
    }
  
    public ArrayList<Books> getAllBooks(){
        
        Books book;
        ArrayList<Books> bookList = null;
        try{
        
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            CallableStatement pstmt = conn.prepareCall("{ call GET_ALL_BOOKS() }");
            ResultSet rs = pstmt.executeQuery();
            bookList = new ArrayList<Books>();
            Encrypter encrypt = new Encrypter();
            while(rs.next())
            {
                book = new Books();
                
                //System.out.println(encrypt.encrypt());
                book.setProductCrypt(encrypt.encrypt(Integer.toString(rs.getInt("productID"))));
                //String str  = Integer.toString(rs.getInt("prodcutID"));
                //System.out.println(str);
                
                book.setProductID(rs.getInt("productID"));
                book.setBookID(rs.getInt("bookID"));
                book.setTitle(rs.getString("title"));
                book.setAuthor(rs.getString("author"));
                book.setPrice(rs.getFloat("price"));
                book.setSynopsis(rs.getString("synopsis"));
                book.setCategory(rs.getString("category"));
                book.setPublisher(rs.getString("publisher"));
                book.setCategory(rs.getString("category"));
                book.setIsbn(rs.getString("isbn"));
                bookList.add(book);
            }
            
            return bookList;
        }
        catch(SQLException e){
        
            return bookList;
        }
            
    }
    public Books getBook(int bookID){
        
        Books book = null;
        
        try{
        
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM books b INNER JOIN products p ON b.productID = p.productID WHERE b.productID = ?");
            pstmt.setInt(1, bookID);
            
            ResultSet rs = pstmt.executeQuery();
            if(rs.next())
            {
                book = new Books();
                book.setProductCrypt(new Encrypter().encrypt(Integer.toString(rs.getInt("productID"))));
                book.setBookID(rs.getInt("bookID"));
                book.setTitle(rs.getString("title"));
                book.setPrice(rs.getFloat("price"));
                book.setAuthor(rs.getString("author"));
                book.setSynopsis(rs.getString("synopsis"));
                book.setCategory(rs.getString("category"));
                book.setPublisher(rs.getString("publisher"));
                book.setCategory(rs.getString("category"));
                book.setIsbn(rs.getString("isbn"));               
            }
            return book;           
        }
        catch(SQLException e){
        
            e.printStackTrace();
            return book;
        }    
    }
}
